package UTGBusiness;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author yuxia
 */
public class IDDManager {

    private Connection conn;

    public IDDManager() {

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/"
                    + "mysql", "root", "12345");
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public boolean hasID(String id){
        try {
            String sql = "SELECT * FROM id WHERE name='" + id + "'";
            Statement stmt = null;
            ResultSet rs = null;
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            return rs.next();
        } catch (SQLException ex) {
            Logger.getLogger(IDDManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    public String getFirmID(String str) {
        String sql = "SELECT firmid FROM id WHERE name='" + str + "'";
        Statement stmt = null;
        ResultSet rs = null;
        try {
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            if(rs.next()){
                return rs.getString("firmid");
            }
        } catch (SQLException ex) {
            Logger.getLogger(IDDManager.class.getName())
                    .log(Level.SEVERE, null, ex);
            //return ;
        }
        return null;
    }

    public String getFirmName(String str) {
        String sql = "SELECT firmname FROM id WHERE name='" + str + "'";
        Statement stmt = null;
        ResultSet rs = null;
        try {
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            if(rs.next()){
                return rs.getString("firmname");
            }
        } catch (SQLException ex) {
            Logger.getLogger(IDDManager.class.getName())
                    .log(Level.SEVERE, null, ex);
            //return ;
        }
        return null;
    }
}
